import Cookies from 'js-cookie'
import lscache from 'lscache';

const TokenKey = 'Authorization'
/*
* 设置setLocalStorage
* */
export function setLocalStorage(key, value) {
    window.localStorage.setItem(key, window.JSON.stringify(value))
}
/*
* 获取getLocalStorage
* */
export function getLocalStorage(key) {
    return window.JSON.parse(window.localStorage.getItem(key) || '[]')
}
/*
* 移除removeLocalStorage
* */
export function removeLocalStorage(key) {
    window.localStorage.removeItem(key)
}
/*
* 全部移除clearLocalStorage
* */
export function clearLocalStorage() {
    window.localStorage.clear()
}
/*
* 设置setSessionStorage
* */
export function setSessionStorage(key, value) {
    window.sessionStorage.setItem(key, window.JSON.stringify(value))
}
/*
* 获取getSessionStorage
* */
export function getSessionStorage(key) {
    return window.JSON.parse(window.sessionStorage.getItem(key) || '[]')
}
/*
* 移除removeSessionStorage
* */
export function removeSessionStorage(key) {
    window.sessionStorage.removeItem(key)
}
/*
* 全部移除clearSessionStorage
* */
export function clearSessionStorage() {
    window.sessionStorage.clear()
}
/*
* 获取getToken
* */
export function getToken(key) {
    return Cookies.get(key)
}
/*
* 设置setToken
* */
export function setToken(key, token) {
    return Cookies.set(key, token)
}
/*
* 移除removeToken
* */
export function removeToken(key) {
    return Cookies.remove(key)
}

/**保存权限 */
export function SetPermission(data) {
    if (!lscache.supported()) {
        alert('浏览器不支持本地储存');
        return;
    }
    lscache.flush();
    lscache.set('module', data, 30000);
}

/**获取所有权限 */
export function GetPermission() {
    return lscache.get('module');
}

/**判断是否具有指定权限 */
export function isPermission(name) {
    var rowArr = lscache.get('module') ? lscache.get('module').userMenuKey : [];
    var isPer = rowArr.find(o => o == name);
    if (isPer == undefined) {
        return false;
    } else {
        return true;
    }
}

export function flushPermission() {
    lscache.flush();
}
